<?php
namespace App\Http\Controllers\AdminApi;
use App\Model\NewsTable;
use App\Repository\Admin\AdminService;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//资讯管理
class NewsController extends ApiController{
	//新闻-列表
    public function getNewsList(){
        $page = \Request::get('page',1);
        $limit = \Request::get('page_num',10);
        $title = \Request::get('title');
        $type = \Request::get('type',0);
        return JsonRespose::done(NewsTable::getNewsList($page,$limit,$type,$title,0));
    }
    //新闻-详情
    public function getNewsInfo(){
        $id = \Request::get('id');
        return JsonRespose::done(NewsTable::getNewsInfo($id));
    }
    //新闻-添加修改
    public function postNewsSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'uid' => AdminService::getAdminId(),
            'title' => $param['title'],
            'type' => $param['type'],
            'tag' => $param['tag'],
            'desc' => $param['desc'],
            'img_url' => $param['img_url'],
            'img_sm_url' => $param['img_sm_url'],
            'source' => $param['source'],
            'content' => $param['content'],
            'status' => $param['status'],//1发布 2保存
            'created_at' => Str::getNowDateTime(),
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = NewsTable::ORM()->where('id', $param['id'])->update($data);
        } else {
            //否则插入数据
            $data['updated_at'] = Str::getNowDateTime();
            $res = NewsTable::ORM()->insertGetId($data);
            //自增id更新为排序id
            NewsTable::ORM()->where('id', $res)->update(['sort' => $res]); //排序id
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
    //新闻-删除
    public function postNewsDel(){
        $id = \Request::get('id');
        //取得当前数据
        $res = NewsTable::ORM()->where('id', $id)->update(['status' => -1]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //新闻-刷新上浮
   	public function postNewsUp(){
        $id = \Request::get('id');
        //取得当前数据
        $res = NewsTable::ORM()->where('id', $id)->first();
        //取得上面一条数据
        $res_top = \DB::select('select * from news WHERE sort>? ORDER BY sort ASC limit 1', [$res->sort]);
        //如果是挤到最后,判断不存在时取出最后一条 $res_top
        if (!isset($res_top[0])) {
            $res_top = \DB::select('select * from news ORDER BY sort ASC limit 1', []);
        }
        if (!isset($res_top[0]) or ($res_top[0]->id == $res->id)) {
            return JsonRespose::success();
        }
        //当前数据和上一条数据 做update 操作 desc
        $s1 = NewsTable::ORM()->where('id', $res->id)->update(array('sort' => $res_top[0]->sort));
        $s2 = NewsTable::ORM()->where('id', $res_top[0]->id)->update(array('sort' => $res->sort));
        if ($s1 and $s2) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //新闻-置顶
    public function postNewsTop(){
        $id = \Request::get('id');
        $res = NewsTable::ORM()->where('id', $id)->update(array('sort_top' => 1));
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //新闻-取消置顶 恢复原位
    public function postNewsTopDel(){
        $id = \Request::get('id');
        //取得当前数据
        $res = NewsTable::ORM()->where('id', $id)->update(array('sort_top' => 0));
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}


